package db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import user.view.LoginView;
import user.view.UserMain;

import config.SystemConfig;

public class DataBaseManager {

	private static Connection connection;
	public static Connection getConnection()
	{
		if(connection==null)
		{
			try {
				Class.forName("com.mysql.jdbc.Driver");
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				LoginView.getInstance().setErrorInfo(e.getMessage());
				e.printStackTrace();
			}
			String connString="jdbc:mysql://"+SystemConfig.ip+":"+SystemConfig.port+"/"+SystemConfig.catalog+"?user="+SystemConfig.user+"&password="+SystemConfig.pwd;
			try {
				connection = DriverManager.getConnection(connString);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				LoginView.getInstance().setErrorInfo("click I'am a vistor Button(ConnectFailed)");
				SystemConfig.offline=true;
				UserMain.getInstance().setConnectFailed();
				System.out.println("offline=true");
				//e.printStackTrace();
			}
           

		}
		return connection;
		
	}
	public static void main(String[] args) {
		Statement statement;
		try {
			statement = DataBaseManager.getConnection().createStatement();
		
		String search = "SELECT * FROM userinfo;";
        ResultSet test = statement.executeQuery(search);
        while(test.next())
        {
        	System.out.println(test.getString(4));
        }
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
